package com.daidai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.daidai.dao.AssociationMemberDao;
import com.daidai.model.AssociationMember;
import com.daidai.model.AssociationMemberDTO;
import com.daidai.service.AssociationMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Author: Mr.Chen
 * Date: 2022/08/01 17:46
 * Description:
 */
@Service
public class AssociationMemberServiceImpl implements AssociationMemberService {

    @Autowired
    private AssociationMemberDao associationMemberDao;
    @Override
    public AssociationMemberDTO findMemberById(int id) {
        return associationMemberDao.findMemberById(1);
    }

    @Override
    public int findAssociationIdByUserId(int userId) {
        return associationMemberDao.selectOne(new LambdaQueryWrapper<AssociationMember>().eq(AssociationMember::getUserId, userId)).getAssociationId();
    }

    //查询会长简介
    @Override
    public String findPresidentDesByAssid(int associationId) {
        return associationMemberDao.selectOne(new LambdaQueryWrapper<AssociationMember>().eq(AssociationMember::getAssociationId, associationId).eq(AssociationMember::getPosition, 1)).getDescription();
    }

    //查询副会长简介
    @Override
    public List<String> findVicePresidentDesByAssid(int associationId) {
        LinkedList<String> linkList = new LinkedList<>();
        List<AssociationMember> list = associationMemberDao.selectList(new LambdaQueryWrapper<AssociationMember>().eq(AssociationMember::getAssociationId, associationId).eq(AssociationMember::getPosition, 2));
        for (AssociationMember member : list) {
            linkList.add(member.getDescription());
        }
        return linkList;
    }

    //查询部长简介
    @Override
    public List<String> findMinisterDesByAssid(int associationId) {
        LinkedList<String> linkList = new LinkedList<>();
        List<AssociationMember> list = associationMemberDao.selectList(new LambdaQueryWrapper<AssociationMember>().eq(AssociationMember::getAssociationId, associationId).eq(AssociationMember::getPosition, 3));
        for (AssociationMember member : list) {
            linkList.add(member.getDescription());
        }
        return linkList;
    }

    @Override
    public List<AssociationMemberDTO> findAllMember() {
        return associationMemberDao.findAllMember();
    }

    @Override
    public List<AssociationMemberDTO> findMemberByUserId(int userId) {
        return associationMemberDao.findMemberByUserId(userId);
    }

    @Override
    public void deleteUser(int id) {
        associationMemberDao.deleteById(id);
    }

    @Override
    public void updateAm(int userId, int position, String description) {
        associationMemberDao.update(new AssociationMember().setUserId(userId).setPosition(position).setDescription(description), new LambdaQueryWrapper<AssociationMember>().eq(AssociationMember::getUserId, userId));
    }

    //根据社团id更新新的会长
    @Override
    public void updatePresidentByAssociationId(int userId, int associationId) {
        associationMemberDao.update(new AssociationMember().setUserId(userId).setPosition(1), new LambdaQueryWrapper<AssociationMember>().eq(AssociationMember::getAssociationId, associationId));
    }
}
